﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.ResumeService;
using Avatar.Model;
using AvatarHelper;
using System.Data.Linq;
using Avatar.MembershipService;
using Avatar.Common;
using System.Web.Security;

public partial class ResumeView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var _currentUser = LoginManager.GetCurrentUser();
        var _resumeUser = Request.QueryString["User"] != null ? Request.QueryString["User"].ToString() : string.Empty;

        using (var _adc = DBHelper.GetDataContext())
        {
            if (Roles.IsUserInRole("个人用户"))
            {
                PersonHelper.BasicInformationJobIntentionCheck();
                _resumeUser = _currentUser.UserName;
            }
            else if (Roles.IsUserInRole("企业用户"))
            {
                ///判断用户服务种类 
                ///1.如果普通会员则不准查看 
                ///2.如果按条计费会员则扣费
                ///3.包月会员可以查看
                var _service = _adc.CompanyService.FirstOrDefault(s => s.Company == _currentUser.UserName);

                if (_service.ServiceTypeID == 1)
                {
                    Helper.ShowMassage("普通用户不能查看简历", "default.aspx");
                }
                else if (_service.ServiceTypeID == 2)
                {
                    var _jobConfig = _adc.JobConfig.FirstOrDefault();
                    if (_jobConfig == null) Helper.ShowMassage("招聘信息相关基本参数未设定，赞不能查看简历。请与客服联系。", "./default.aspx");

                    var _account = _adc.CompanyFinancialAccount.FirstOrDefault(a => a.Company == _currentUser.UserName);
                    if (_account == null || _account.Balance < 2)
                    {
                        Helper.ShowMassage("余额不足请充值后再查看简历", "./CompanyHome.aspx");
                    }
                    else
                    {
                        var _log = new CompanyFinancialAccountLog();
                        _log.Company = _currentUser.UserName;
                        _log.AccountCategoryID = _adc.CompanyFinancialAccountLogCategory.FirstOrDefault(c => c.CategoryName == "查看简历").ID;
                        _log.Money = Convert.ToDecimal(_jobConfig.Price);
                        _log.Date = DateTime.Now;
                        _log.Balance = _account.Balance - Convert.ToDecimal(_jobConfig.Price);
                        _adc.CompanyFinancialAccountLog.InsertOnSubmit(_log);
                        _adc.SubmitChanges();

                        _account.Balance = _account.Balance - Convert.ToDecimal(_jobConfig.Price);
                        _adc.SubmitChanges();
                    }
                }
            }

            if (!string.IsNullOrEmpty(_resumeUser))
            {
                var _resume = _adc.Resume.Where(r => r.User == _resumeUser).FirstOrDefault();
                if (_resume == null)
                {
                    Helper.ShowMassage("简历不存在。", "Default.aspx");
                }
                else
                {
                    var _info = _resume.PersonalBasicInfo;
                    if (_info == null)
                    {
                        Helper.ShowMassage("简历信息不完整，无法查看", "Default.aspx");
                    }
                    else
                    {
                        lbl_AccountLocation.Text = _adc.Area.FirstOrDefault(a => a.ID == _info.AccountLocation).Name;
                        lbl_Birthday.Text = _info.Birthday.ToShortDateString();
                        lbl_Email.Text = string.IsNullOrEmpty(_info.Email) ? "" : _info.Email;
                        lbl_Living.Text = _adc.Area.FirstOrDefault(a => a.ID == _info.LivingCity).Name;
                        lbl_Marry.Text = _info.MaritalStatus == null ? "保密" : _info.MaritalStatus.Status;
                        lbl_Sex.Text = _info.Sex.Name == "男女不限" ? "" : _info.Sex.Name+" | ";
                        lbl_TelNumber.Text = _info.TelNumber;
                        lbl_UserName.Text = _info.Name;
                        lbl_WorkingAge.Text = string.IsNullOrEmpty(_info.WorkingAge.Age)?"":_info.WorkingAge.Age+" 工作经验";

                        if (_resume.SelfEvaluation != null)
                        {
                            panel_SelfEvaluation.Visible = true;
                            lbl_SelfEvaluation.Text = _resume.SelfEvaluation.Evaluation;
                        }
                        else
                        {
                            panel_SelfEvaluation.Visible = false;
                        }

                        var _intention = _resume.JobIntention;
                        if (_intention != null)
                        {
                            lbl_JobType.Text = _intention.JobType.Name;
                            lbl_Area.Text = _intention.Area.Name;
                            foreach (var _i in _intention.JobIntentionIndustry)
                            {
                                lbl_Industry.Text += _adc.Industry.FirstOrDefault(i => i.ID == _i.IndustryID).Name + " ";
                            }
                            lbl_Position.Text = (_intention.Position == null || string.IsNullOrEmpty(_intention.PositionName)) ? "未设置" : _intention.Position.Name + "&nbsp&nbsp" + _intention.PositionName;
                            lbl_SalaryWant.Text = _intention.SalaryYear.Salary;
                            lbl_TimeOnDuty.Text = _intention.TimeOnDuty.Name;
                            lbl_Attitude.Text = _intention.Attitude.JobAttitude;
                        }
                        else
                        {
                            Helper.ShowMassage("简历信息不完整，无法查看", "Default.aspx");
                        }

                        if (_resume.WorkExperience.Count > 0)
                        {
                            rpt_WorkExperience.DataSource = _resume.WorkExperience;
                            rpt_WorkExperience.DataBind();

                            panel_WorkExperience.Visible = true;
                        }
                        else
                        {
                            panel_WorkExperience.Visible = false;
                        }

                        if (_resume.Education.Count > 0)
                        {
                            rpt_Education.DataSource = _resume.Education;
                            rpt_Education.DataBind();

                            panel_Education.Visible = true;
                        }
                        else
                        {
                            panel_Education.Visible = false;
                        }

                        if (_resume.TrainingExperience.Count > 0)
                        {
                            rpt_TrainingExperience.DataSource = _resume.TrainingExperience;
                            rpt_TrainingExperience.DataBind();

                            panel_TrainingExperience.Visible = true;
                        }
                        else
                        {
                            panel_TrainingExperience.Visible = false;
                        }

                        if (_resume.LanguageAbility.Count > 0)
                        {
                            panel_Language.Visible = true;

                            rpt_Language.DataSource = _resume.LanguageAbility;
                            rpt_Language.DataBind();

                            var _languageLevel = _resume.LanguageLevel;
                            if (_languageLevel != null)
                            {
                                lbl_English.Text = _languageLevel.EnglishLevel.Level;
                                lbl_Toefl.Text = _languageLevel.TOEFL + "分";
                                lbl_Gre.Text = _languageLevel.GRE + "分";
                                lbl_Japanese.Text = _languageLevel.JapaneseLevel.Level;
                                lbl_Gmat.Text = _languageLevel.GMAT + "分";
                                lbl_Ielts.Text = _languageLevel.IELTS + "分";
                                panel_LanguageLevel.Visible = true;
                            }
                            else
                            {
                                panel_LanguageLevel.Visible = false;
                            }
                        }
                        else
                        {
                            panel_Language.Visible = false;
                        }
                        if (!Roles.IsUserInRole("个人用户"))
                        {
                            _resume.ViewCount += 1;
                        }
                        _adc.SubmitChanges();
                    }
                }
            }
        }
    }

    protected string GetDate(DateTime _beginDate, DateTime _overDate)
    {
        string _b, _o;
        _b = _beginDate.Year + "年" + _beginDate.Month + "月 至 ";
        if (_overDate == DateTime.MinValue)
            _o = "现在";
        else
            _o = _overDate.Year + "年" + _overDate.Month + "月";

        return _b + _o;
    }
}
